import java.util.HashMap;
import java.util.Objects;

class Solution {
    public String minWindow(String s, String t) {
        HashMap<Character, Integer> hashMap1 = new HashMap<>();
        HashMap<Character, Integer> hashMap2 = new HashMap<>();

        char[] arr = t.toCharArray();
        for (char c : arr) {
            hashMap1.put(c, hashMap1.getOrDefault(c, 0) + 1);
        }
        int left = 0, right = 0, count = 0;
        int len = Integer.MAX_VALUE;
        int begin = -1;
        while (right < s.length()) {
            char c1 = s.charAt(right++);
            if (hashMap1.containsKey(c1)) {
                hashMap2.put(c1, hashMap2.getOrDefault(c1, 0) + 1);
                if (Objects.equals(hashMap2.get(c1), hashMap1.get(c1))) {
                    count++;
                }
            }
            while (hashMap1.size() == count) {
                if (right - left < len) {
                    len = right - left;
                    begin = left;
                }
                char c2 = s.charAt(left++);
                if (hashMap2.containsKey(c2)) {
                    if (Objects.equals(hashMap2.get(c2), hashMap1.get(c2))) {
                        count--;
                    }
                    hashMap2.put(c2, hashMap2.get(c2) - 1);
                }
            }
        }
        if (begin == -1) {
            return "";
        }
        return s.substring(begin, begin + len);
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.minWindow("ADOBECODEBANC", "ABC"));
    }
}